package work.sajor.wechatpush.service;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.tmt.v20180321.TmtClient;
import com.tencentcloudapi.tmt.v20180321.models.*;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class Translate {
    @Value("${tencent.secretId}")
    private String tencent_secretId;
    @Value("${tencent.secretKey}")
    private String tencent_secretKey;
    @Value("${love.name}")
    private String love_name;
    private static String secretId;
    private static String secretKey;
    private static String name;
    @PostConstruct
    public void getsecretId() {
        secretId = this.tencent_secretId;
        secretKey = this.tencent_secretKey;
        name=this.love_name;
    }

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(Translate.class);

    public static String enToZh(String content) {
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("tmt.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            TmtClient client = new TmtClient(cred, "ap-shanghai", clientProfile);
            TextTranslateRequest req = new TextTranslateRequest();
            req.setSourceText(content);
            req.setSource("en");
            req.setTarget("zh");
            req.setProjectId(0L);
            TextTranslateResponse resp = client.TextTranslate(req);
            return resp.getTargetText();
        } catch (TencentCloudSDKException e) {
            logger.info("腾讯云接口请求错误，错误信息：{}", e.toString());
            return null;
        }
    }

    public static String zhToEn(String content) {
        try {
            Credential cred = new Credential(secretId, secretKey);
            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setEndpoint("tmt.tencentcloudapi.com");
            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setHttpProfile(httpProfile);
            TmtClient client = new TmtClient(cred, "ap-shanghai", clientProfile);
            TextTranslateRequest req = new TextTranslateRequest();
            req.setSourceText(content);
            req.setSource("zh");
            req.setTarget("en");
            req.setProjectId(0L);
            req.setUntranslatedText(name);
            TextTranslateResponse resp = client.TextTranslate(req);
            return resp.getTargetText();
        } catch (TencentCloudSDKException e) {
            logger.info("腾讯云接口请求错误，错误信息：{}", e.toString());
            return null;
        }
    }
}
